package com.example.ch03myrealmhiro.controller;


import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class UserController {


    @RequestMapping("/reallogin")
    public String login(String username,String password,boolean remember){
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        remember=true;
        Subject subject = SecurityUtils.getSubject();
        if (remember){
            token.setRememberMe(remember);
        }
        try {
            subject.login(token);
            return "index";//登陆成功
        } catch (AuthenticationException e) {
            return "login";
        }

    }

    @RequestMapping("/register")
    public String register(String username,String password){
        //注册加盐 加密
        String salt="1";
        Md5Hash md5Hash = new Md5Hash(password,salt);
        System.out.println(md5Hash.toHex());
        System.out.println(md5Hash);
        return null;

    }


}
